//
//  BMHomeViewController.m
//  EnjogFood
//
//  Created by 王芳 on 17/4/30.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "BMHomeViewController.h"
#import "ICSegmentView.h"
#import "EFCatogeryListItem.h"
#import "EFAutoScorllItem.h"
#import "EFBannerItem.h"
#import "EFFeatureItemList.h"
#import "EFSaleListItem.h"
#import "EFSepecialItem.h"

#import "EFCollectionView.h"
#import "EFCatogeryTotalItem.h"
#import "EFAutoTotalScrollItem.h"
#import "EFMessageTotalItem.h"
#import "EFBannerTotalItem.h"
#import "EFSaleTotalItem.h"
#import "EFPromotionTotalItem.h"
#import "EFFeatureTotalItem.h"
#import "EFCarouseTotalItem.h"
#import "EFSpecialTotalItem.h"
#import "EFPromotionItem.h"
#import "SQHomeMenuListItem.h"
#import "SQHomeMenuItem.h"

#import "EFDeliveryAddressViewController.h"
#import "EFSearchAddressViewController.h"
#import "EFUserLocationItem.h"
#import "EFMyDeliveryAddressViewItem.h"

#import "EFBannerItem.h"

#import "EFShoppingCartViewController.h"
#import "EFSearchViewController.h"
#import "EFGoodsCategoryView.h"
#import "EFShoppingCartItem.h"

#define transparentWidth 20
#define NavTitleViewWidth 290
#define NavTitleHeight 30

@interface BMHomeViewController ()<EFLocationManagerDelegate,UIScrollViewDelegate>
@property (strong, nonatomic) ICSegmentView *segmentView;//segmentView菜单栏暂时隐藏了
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) EFCollectionView *collectionTableView;
@property (strong, nonatomic) UILabel *emptyLabel;    //数据为空时的label

//导航栏容器view
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic,strong)  UIView * searchView;
@property (nonatomic, strong) UIButton *searchBtn;
@property (nonatomic, strong) UIImageView *searchImgView;
@property (nonatomic, strong) UILabel *searchLabel;
@property (strong, nonatomic) UIView *leftBtnView;
@property (strong, nonatomic) UIButton *arrowBtn;
@property (strong, nonatomic) UIButton *locationBtn;
@property (strong, nonatomic) UIButton *scanBtn;

@property (strong, nonatomic) NSMutableArray *catogeryArray;
@property (strong, nonatomic) NSMutableArray *catogeryNames;
@property (strong, nonatomic) NSMutableArray *catogeryViews;

@property (strong, nonatomic) EFCatogeryTotalItem *catogeryTotalItem;
@property (strong, nonatomic) EFCarouseTotalItem *carouseTotalItem;
@property (strong, nonatomic) EFSpecialTotalItem *specialTotalItem;
@property (strong, nonatomic) EFMessageTotalItem *messsageTotalItem;
@property (strong, nonatomic) EFBannerTotalItem *bannerTotalItem;
@property (strong, nonatomic) EFSaleTotalItem *saleTotalItem;
@property (strong, nonatomic) EFPromotionTotalItem *promotionTotalItem;
@property (strong, nonatomic) EFFeatureTotalItem *featureTotalItem;

@property (strong, nonatomic) NSMutableArray *tableViewDataSource;





@property (assign, nonatomic) NSInteger shopcartCount;
@property (assign, nonatomic) NSInteger homeRequestCount;
@property(copy,nonatomic) NSString *locationStr;
@end

@implementation BMHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.shopcartCount = 0;
    self.homeRequestCount = 0;
    self.locationStr = @"北京";
    self.catogeryArray = [NSMutableArray array];
    self.catogeryNames = [NSMutableArray array];
    self.catogeryViews = [NSMutableArray array];
    self.tableViewDataSource = [NSMutableArray array];
    self.bannerTotalItem = [[EFBannerTotalItem alloc] init];
    // [self fakeData];
//    [self.view addSubview:self.segmentView];//隐藏顶部菜单栏
//    self.segmentView.showsHorizontalScrollIndicator = false;
//    [self.view addSubview:self.scrollView];
//    [self.scrollView addSubview:self.collectionTableView];
//    self.scrollView.top = self.segmentView.bottom;
    self.scrollView.top = 0;
    [self.view addSubview:self.collectionTableView];
    self.collectionTableView.top = 0;
    [AppContext instance].locationManager.locationDelegate = self;
    
    
//改变方案，只保留中间titleView
    [self addTopSearchView];
    
    //监听地址选择
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addressSelected:) name:@"selectLocationItem" object:nil];
    //监听定位地址改变
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLocation:) name:IC_CHANGE_LOCATION_ADDRESS_NOTIFICATION object:nil];

    
    __weak typeof(self) weakSelf = self;
    [self.collectionTableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf requestHomeInfor:weakSelf.locationStr];
    }];
    //增加一下定位请求
     [APP_CONTEXT.locationManager stopLocation];
    [APP_CONTEXT.locationManager startLocation];
    
    //由于模拟器定位失败暂时固定写死地址
    [self.locationBtn setTitle:self.locationStr forState:UIControlStateNormal];
    [self.locationBtn sizeToFit];
    [self locationViewReLayout];
    [self requestHomeInfor:self.locationStr];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
  
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.collectionTableView stopTimer];
    [self.collectionTableView stopScrollView];
    [APP_CONTEXT.locationManager stopLocation];
}
- (void)locationViewReLayout{
    self.leftBtnView.width = self.arrowBtn.width + self.locationBtn.width;
    self.arrowBtn.left = 0;
    self.arrowBtn.top = (self.leftBtnView.height - self.arrowBtn.height)/2;
    self.locationBtn.left = self.arrowBtn.right;
    self.locationBtn.top = (self.leftBtnView.height - self.locationBtn.height)/2;
    
}

- (void)requestHomeInfor:(NSString *)position{
    [self showHUD];
    __weak typeof(self) weakSelf = self;
    self.emptyLabel.hidden = YES;
    [APP_CONTEXT.net getHomeInfor:position OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        [weakSelf.collectionTableView.header endRefreshing];
        [weakSelf.HUD hide:YES];
        if (code == 0) {//成功
            [weakSelf hideNoNetWorkView];
            weakSelf.emptyLabel.hidden = YES;
            [weakSelf.tableViewDataSource removeAllObjects];
            [weakSelf.collectionTableView.dataSourceArray removeAllObjects];
            [weakSelf.collectionTableView.collectionTableView reloadData];
            [weakSelf.bannerTotalItem.bannerList removeAllObjects];
            
            if ([info objectForKey:@"whCode"] != nil) {
                APP_CONTEXT.whCode = [info objectForKey:@"whCode"];
            }
            if ([info objectForKey:@"isLocalDelivery"] != nil) {
                APP_CONTEXT.isLocalDelivery = [[info objectForKey:@"isLocalDelivery"] boolValue];
            }
            if ([info objectForKey:@"bannerList"] != nil) {
                NSArray *bannerArray = [info objectForKey:@"bannerList"];
                for (NSDictionary *dic in bannerArray) {
                    EFBannerItem *bannerItem = [EFBannerItem yy_modelWithDictionary:dic];
                    [weakSelf.bannerTotalItem.bannerList addObject:bannerItem];
                }
            }
            if ([info objectForKey:@"categoryList"] != nil) {
                
                /*
                weakSelf.catogeryTotalItem = [EFCatogeryTotalItem yy_modelWithDictionary:[info objectForKey:@"categoryList"]];
                [weakSelf handleCatogeryName];
                weakSelf.scrollView.contentSize = CGSizeMake(self.catogeryNames.count * kScreenWidth, kScreenHeight);
                [weakSelf.segmentView setSegmentArray:self.catogeryNames SelectIndex:^(NSInteger index) {
                    //增加分类页面
                    //view加载网络请求
                     [weakSelf createSegmengView:index];
                    if (index != 0) {
                        EFGoodsCategoryView *v = [self.catogeryViews objectAtIndex:(index - 1)];
                        v.page = 1;
                        [v initData];
                    }
                }];
                if ([weakSelf totalWidth:self.catogeryNames] > kScreenWidth) {
                    self.segmentView.contentSize = CGSizeMake([self totalWidth:self.catogeryNames], 40);
                }
                 */
            }
            //轮播图
            if ([info objectForKey:@"carouselList"] != nil) {
                weakSelf.carouseTotalItem = [EFCarouseTotalItem yy_modelWithDictionary:[info objectForKey:@"carouselList"]];
                if (weakSelf.carouseTotalItem.isVisible == YES) {
                    [weakSelf.tableViewDataSource addObject:weakSelf.carouseTotalItem];
                }
            }
            //消息
            if ([info objectForKey:@"messageList"] != nil) {
                weakSelf.messsageTotalItem = [EFMessageTotalItem yy_modelWithDictionary:[info objectForKey:@"messageList"]];
                if (weakSelf.messsageTotalItem.isVisible == YES && weakSelf.messsageTotalItem.list.count > 0) {
                    [weakSelf.tableViewDataSource addObject:weakSelf.messsageTotalItem];
                }
            }
        
            //限时优惠
            if ([info objectForKey:@"specialOfferList"] != nil) {
                
                weakSelf.specialTotalItem = [EFSpecialTotalItem yy_modelWithDictionary:[info objectForKey:@"specialOfferList"]];
                if (weakSelf.specialTotalItem.isVisible == YES) {
                    [weakSelf.tableViewDataSource addObject:weakSelf.specialTotalItem];
                }
            }
            //增加banner
            if (weakSelf.bannerTotalItem.bannerList.count > 0 ) {
                [weakSelf.tableViewDataSource addObject:weakSelf.bannerTotalItem.bannerList[0]];
            }
          
            //限时秒杀
            if ([info objectForKey:@"saleList"] != nil) {
                weakSelf.saleTotalItem = [EFSaleTotalItem yy_modelWithDictionary:[info objectForKey:@"saleList"]];
                if (weakSelf.saleTotalItem.isVisible == YES) {
                    [weakSelf.tableViewDataSource addObject:weakSelf.saleTotalItem];
                }
            }
            //促销
            if ([info objectForKey:@"eventPromotion"] != nil) {
                weakSelf.promotionTotalItem = [EFPromotionTotalItem yy_modelWithDictionary:[info objectForKey:@"eventPromotion"]];
                if (weakSelf.promotionTotalItem.isVisible == YES) {
                    for (EFPromotionItem *pItem in weakSelf.promotionTotalItem.list) {
                        [weakSelf.tableViewDataSource addObject:pItem];
                    }
                }
            }
            //增加banner
            if (weakSelf.bannerTotalItem.bannerList.count > 1) {
                [weakSelf.tableViewDataSource addObject:weakSelf.bannerTotalItem.bannerList[1]];
            }
            //特色推荐
            if ([info objectForKey:@"featureItemList"] != nil) {
                weakSelf.featureTotalItem = [EFFeatureTotalItem yy_modelWithDictionary:[info objectForKey:@"featureItemList"]];
                if (weakSelf.featureTotalItem.isVisible) {
                    NSMutableArray *array = weakSelf.featureTotalItem.list;
                    for (EFFeatureItemList *featureItem in array) {
                        [weakSelf.tableViewDataSource addObject:featureItem];
                    }
                }
            }
            //增加banner
            if (weakSelf.bannerTotalItem.bannerList.count > 2) {
                [weakSelf.tableViewDataSource addObject:weakSelf.bannerTotalItem.bannerList[2]];
            }
            
            //赋值给tableView 的DataSource
            weakSelf.collectionTableView.dataSourceArray = weakSelf.tableViewDataSource;
            [weakSelf.collectionTableView.collectionTableView reloadData];
        }else{
            //失败 (这里为了测试暂时注释掉了:正式数据时显示)
//            [weakSelf.tableViewDataSource removeAllObjects];
//            [weakSelf.collectionTableView.dataSourceArray removeAllObjects];
//            [weakSelf.collectionTableView.collectionTableView reloadData];
//            [weakSelf showEmptyLabel];
            
            
            //test
            [self testEFCollectionViewUI];
        }
        if (self.shopcartCount == 0) {
             [self getShopCartData];//同步购物车数据
            self.shopcartCount ++;
        }
        
    }];
}

- (void)showEmptyLabel{
    self.emptyLabel.text =  @"暂无内容";
    [self.emptyLabel sizeToFit];
    self.emptyLabel.bottom =(kScreenHeight - kStatusBarHeight)/2 - 30.f;
    self.emptyLabel.centerX = kScreenWidth/2.f;
    self.emptyLabel.hidden = NO;
    [self.collectionTableView addSubview:self.emptyLabel];
}

- (UILabel *)emptyLabel{
    if (!_emptyLabel) {
        _emptyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _emptyLabel.backgroundColor = [UIColor clearColor];
        _emptyLabel.font = [UIFont systemFontOfSize:12.f];
        _emptyLabel.textColor = RGBCOLOR(0x9D9AAA);
        _emptyLabel.hidden = YES;
    }
    return _emptyLabel;
}

- (void)addTopSearchView {
    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth,44)];
    self.navigationItem.titleView = self.containerView;
//    self.containerView.backgroundColor = [UIColor redColor];
    //左按钮
    [self.leftBtnView addSubview:self.arrowBtn];
    [self.leftBtnView addSubview:self.locationBtn];
    [self.containerView addSubview:self.leftBtnView];
    [self.containerView addSubview:self.searchView];
    [self.containerView addSubview:self.scanBtn];
    [self.searchView addSubview:self.searchImgView];
    [self.searchView addSubview:self.searchLabel];
    [self.searchView addSubview:self.searchBtn];
    [self.locationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(4);
        make.height.mas_equalTo(22);
    }];
    [self.arrowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.locationBtn.mas_right).offset(5);
        make.top.mas_equalTo(4);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(22);
    }];
    [self.leftBtnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.centerY.mas_equalTo(self.containerView);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(60);
    }];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.leftBtnView.mas_right).offset(15);
        make.centerY.mas_equalTo(self.containerView);
        make.height.mas_equalTo(30);
        make.right.mas_equalTo(-53);
    }];
    [self.searchView.layer setCornerRadius:10];
    self.searchView.layer.masksToBounds = true;
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    [self.searchImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.centerY.mas_equalTo(self.searchView);
    }];
    [self.searchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.searchImgView.mas_right).offset(8);
        make.centerY.mas_equalTo(self.searchView);
    }];
    
    [self.scanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(22);
        make.centerY.mas_equalTo(self.containerView);
    }];

    /*
    //设置圆角
    [self.searchView.layer setCornerRadius:5.5];
    self.searchTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 250, 30)];
    self.searchTF.borderStyle = UITextBorderStyleNone;
    self.searchTF.placeholder = @"搜商品找品牌";
    //搜索框背景
    self.searchTF.background = [UIImage imageNamed:@"search_btn_Rectangle_city"];
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(60, 0, 28, 32)];
    UIImageView * searchImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nav_icon_search_default"]];
    searchImageView.frame = CGRectMake(10, 7, 18, 18);
    [leftView addSubview:searchImageView];
    self.searchTF.leftView = leftView;
    self.searchTF.leftViewMode = UITextFieldViewModeAlways;

    self.searchTF.font = [UIFont systemFontOfSize:12.f];
    [self.searchTF addTarget:self action:@selector(textFieldEditDidChanged:) forControlEvents:UIControlEventEditingChanged];
    [_searchView addSubview:self.searchTF];
    */
}

- (void)addMeNuView {
    UIView *containerView = [[UIView alloc] init];
    [self.view addSubview:containerView];
    containerView.backgroundColor = [UIColor whiteColor];
    containerView.layer.borderWidth = 1;
    containerView.layer.borderColor = [UIColor grayColor].CGColor;
    
}
- (void)createSegmengView:(NSInteger)index{
    //因为第一个是首页
    self.scrollView.contentOffset = CGPointMake(index *kScreenWidth, 0);

//    [UIView animateWithDuration:0.3f animations:^{
//            } completion:nil];
}

- (void)testEFCollectionViewUI {
    //测试轮播图
    NSMutableArray *testArray = [NSMutableArray arrayWithCapacity:3];
    EFAutoScorllItem *testItem = [EFAutoScorllItem new];
    EFAutoScorllItem *testItem1 = [EFAutoScorllItem new];
    EFAutoScorllItem *testItem2 = [EFAutoScorllItem new];
    //TODO:初步判断不支持imgUrl，等抽时间仔细求证
    NSString *testUrlStr = @"123123";
    testItem.id = @"1";
    testItem.img = testUrlStr;
    testItem1.id = @"2";
    testItem1.img = testUrlStr;
    testItem2.id = @"3";
    testItem2.img = testUrlStr;
    [testArray addObject:testItem];
    [testArray addObject:testItem1];
    [testArray addObject:testItem2];

    self.carouseTotalItem = [EFCarouseTotalItem new];
    self.carouseTotalItem.list = testArray;
    [self.tableViewDataSource addObject:self.carouseTotalItem];
    self.collectionTableView.dataSourceArray = self.tableViewDataSource;
    [self.collectionTableView.collectionTableView reloadData];
}

- (void)testMenuTableUI {
    NSMutableArray *testArray = [NSMutableArray arrayWithCapacity:4];
    SQHomeMenuItem *testItem = [SQHomeMenuItem new];
    testItem.img = @"test_1";
    testItem.title = @"教育信息";
    SQHomeMenuItem *testItem1 = [SQHomeMenuItem new];
    testItem1.img = @"test_2";
    testItem1.title = @"美容美体";
    SQHomeMenuItem *testItem2 = [SQHomeMenuItem new];
    testItem2.img = @"test_1";
    testItem2.title = @"租房";
    SQHomeMenuItem *testItem3 = [SQHomeMenuItem new];
    testItem3.img = @"test_2";
    testItem3.title = @"松鼠贷";
    [testArray addObject:testItem];
    [testArray addObject:testItem1];
    [testArray addObject:testItem2];
    [testArray addObject:testItem3];
    SQHomeMenuListItem *item = [SQHomeMenuListItem new];
    item.isVisible = YES;
    item.list = testArray;
    [self.tableViewDataSource addObject:item];
    self.collectionTableView.dataSourceArray = self.tableViewDataSource;
    [self.collectionTableView.collectionTableView reloadData];

}
- (void)handleCatogeryName{
    [self.catogeryNames removeAllObjects];
    [self.catogeryViews removeAllObjects];
    //删除上次添加的
    for (UIView *view in self.scrollView.subviews) {
        if ([view isKindOfClass:[EFGoodsCategoryView class]]) {
            [view removeFromSuperview];
        }
    }
    [self.catogeryNames addObject:@"首页"];
    for (NSInteger index = 0; index < self.catogeryTotalItem.list.count; index ++) {
        EFCatogeryListItem *item = [self.catogeryTotalItem.list objectAtIndex:index];
        [self.catogeryNames addObject:item.name];
        EFGoodsCategoryView *v = [[EFGoodsCategoryView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight - 40 - 50) categoryId:item.id];
        [self.catogeryViews addObject:v];
        
        v.origin = CGPointMake((index+1) * kScreenWidth, 0);
        [self.scrollView addSubview:v];
    }
}

- (ICSegmentView *)segmentView{
    if (!_segmentView) {
        _segmentView = [[ICSegmentView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    }
    return _segmentView;
}

- (EFCollectionView *)collectionTableView{
    if (!_collectionTableView) {
        _collectionTableView = [[EFCollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    }
    return _collectionTableView;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
    }
    return _scrollView;
}


//未使用
- (CGFloat)totalWidth:(NSMutableArray *)array{
    CGFloat total = 0;
    for (NSString *str in array) {
        CGFloat width = [self getWidthWithTitle:str];
        total += width + 25;
    }
    total += 25;
    return total;
}

- (CGFloat)getWidthWithTitle:(NSString *)title{
    UILabel *tempLabel = [[UILabel alloc] init];
    tempLabel.font = [UIFont systemFontOfSize:13];
    tempLabel.text = title;
    [tempLabel sizeToFit];
    
    return tempLabel.width;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView *)leftBtnView{
    if (!_leftBtnView) {
        _leftBtnView = [[UIView alloc] init];
        _leftBtnView.backgroundColor = [UIColor clearColor];
        _leftBtnView.userInteractionEnabled = YES;
    }
    return _leftBtnView;
}

- (UIButton *)locationBtn{
    if (!_locationBtn) {
        _locationBtn = [[UIButton alloc] init];
        [_locationBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _locationBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_locationBtn addTarget:self action:@selector(presentAddressPage) forControlEvents:UIControlEventTouchUpInside];
    }
    return _locationBtn;
}

- (UIButton *)arrowBtn{
    if (!_arrowBtn) {
        _arrowBtn = [[UIButton alloc]init];
        [_arrowBtn setImage:IMG(@"nav_icon_down_default") forState:UIControlStateNormal];
        [_arrowBtn addTarget:self action:@selector(presentAddressPage) forControlEvents:UIControlEventTouchUpInside];
    }
    return _arrowBtn;
}

- (UIView *)searchView {
    if (!_searchView) {
        _searchView = [[UIView alloc]init];
        _searchView.backgroundColor = [UIColor jc_colorWithHexString:@"ffffff" alpha:1];
    }
    return _searchView;
}

- (UIButton *)searchBtn {
    if (!_searchBtn) {
        _searchBtn = [[UIButton alloc] init];
        [_searchBtn addTarget:self action:@selector(searchProduct) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}

- (UIImageView *)searchImgView {
    if (!_searchImgView) {
        _searchImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"nav_icon_search_default"]];
    }
    return _searchImgView;
}

- (UILabel *)searchLabel {
    if (!_searchLabel) {
        _searchLabel = [[UILabel alloc] init];
        _searchLabel.backgroundColor = [UIColor clearColor];
        _searchLabel.text = @"请输入您寻找的课程/机构名称";
        _searchLabel.font = [UIFont systemFontOfSize:12.f];
        _searchLabel.textColor = RGBCOLOR(0x9D9AAA);
    }
    return _searchLabel;
}


- (UIButton *)scanBtn{
    if (!_scanBtn) {
        _scanBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
        [_scanBtn setImage:IMG(@"nav_scan") forState:UIControlStateNormal];
        [_scanBtn addTarget:self action:@selector(scanBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _scanBtn;
}


- (void)scanBtnClick{
    //扫码
    
    
    //若没有登录需登录
//    if (APP_CONTEXT.accountManager.accountInfo.token.length > 0) {
//        EFShoppingCartViewController *shopcart = [[EFShoppingCartViewController alloc] initWithType:0];
//        [ICNavigator pushViewController:shopcart animated:YES];
//    }else{
//        [ICNavigator openLoginWithCompleteBlock:^{
//        }];
//    }
    
}

//弹出地址搜索页面
- (void)presentAddressPage{
    EFDeliveryAddressViewController *deliveryVC = [[EFDeliveryAddressViewController alloc] init];
    [ICNavigator presentViewController:deliveryVC animated:YES];
}

//是否弹出过定位请求
- (void)startLocationg:(BOOL)isCheck{
    
}

#pragma mark EFLocationManagerDelegate
- (void)getCurrentLocationName:(NSString *)currentAddress location:(CLLocationCoordinate2D)location{
    NSLog(@"展示地址 %@",currentAddress);
    [self.locationBtn setTitle:currentAddress forState:UIControlStateNormal];
    [self.locationBtn sizeToFit];
    [self locationViewReLayout];
    //获取定位信息 刷新页面
    NSString *postionStr = [NSString stringWithFormat:@"%f,%f",location.longitude,location.latitude];
    self.locationStr = postionStr;
    if (self.homeRequestCount >=2) {
        return;
    }
    self.homeRequestCount ++;
//    if (self.homeRequestCount >= 1) {
//        self.homeRequestCount = 0;
         [APP_CONTEXT.locationManager stopLocation];
         [self requestHomeInfor:postionStr];
//    }
   
}
- (void)changeLocation:(NSNotification *)info{
    EFMyDeliveryAddressViewItem * item = info.object;
    [self.locationBtn setTitle:item.addressDetail forState:UIControlStateNormal];
    [self.locationBtn sizeToFit];
    [self locationViewReLayout];
    //获取定位信息 刷新页面
//    NSString *postionStr = [NSString stringWithFormat:@"%f,%f",lItem.userCoordinate.longitude,lItem.userCoordinate.latitude];
//    [self requestHomeInfor:postionStr];
}

- (void)addressSelected:(NSNotification *)info{
    EFUserLocationItem *lItem = info.object;
    [self.locationBtn setTitle:lItem.detailAddress forState:UIControlStateNormal];
    [self.locationBtn sizeToFit];
    [self locationViewReLayout];
    //获取定位信息 刷新页面
    NSString *postionStr = [NSString stringWithFormat:@"%f,%f",lItem.userCoordinate.longitude,lItem.userCoordinate.latitude];
    [self requestHomeInfor:postionStr];
}


#pragma mark -UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger index = self.scrollView.contentOffset.x/kScreenWidth;
    if (scrollView.contentOffset.x == index * kScreenWidth) {
        [self.segmentView setSeletIndex:index];
    }
}



//原购物车代码暂时废弃
#pragma mark -获取购物车数据(废弃)
-(void)getShopCartData{
    int type = 0; //商城
    [APP_CONTEXT.net getCartListWithIsLocalDelivery:APP_CONTEXT.isLocalDelivery whCode:APP_CONTEXT.whCode type:type OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        self.HUD.hidden = YES;
        if (code == 0) {
            NSArray *list = [info valueForKey:@"list"];
            [APP_CONTEXT.shoppingCartManager.storeProductsCart removeAllObjects];
            if (list.count != 0  ) {
                for (NSDictionary *dic in list) {
                    EFShoppingCartItem *item = [EFShoppingCartItem yy_modelWithDictionary:dic];
                    item.type = [NSNumber numberWithInteger:type];
                    [APP_CONTEXT.shoppingCartManager addToStoreProduct:[item castToSpecialItem ]];
                }
            }
        }
    }];
    type = 1; //快速扫描
    [APP_CONTEXT.net getCartListWithIsLocalDelivery:APP_CONTEXT.isLocalDelivery whCode:APP_CONTEXT.whCode type:type OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        self.HUD.hidden = YES;
        if (code == 0) {
            NSArray *list = [info valueForKey:@"list"];
            [APP_CONTEXT.shoppingCartManager.scanProductsCart removeAllObjects];
            if (list.count != 0  ) {
                for (NSDictionary *dic in list) {
                    EFShoppingCartItem *item = [EFShoppingCartItem yy_modelWithDictionary:dic];
                    item.type = [NSNumber numberWithInteger:type];
                    [APP_CONTEXT.shoppingCartManager addToScanProduct:[item castToSpecialItem ]];
                }
            }
        }
    }];
}

@end
